package com.qlu.syq.controller.admin;

import com.qlu.syq.entity.admin.Place;
import com.qlu.syq.page.admin.Page;
import com.qlu.syq.service.admin.PlaceService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: PlaceController
 * @Description:
 * @Author: ZPF
 * @Date: 2021/5/20 11:28
 **/
@RequestMapping("/admin/place")
@Controller
public class PlaceController {

    @Autowired
    private PlaceService placeService;


    /**
     * 客户管理列表页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView list(ModelAndView model) {
        model.setViewName("place/list");
        return model;
    }

    /**
     * 客户信息添加操作
     *
     * @param place
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> add(Place place) {
        Map<String, String> ret = new HashMap<>();
        if (place == null) {
            ret.put("type", "error");
            ret.put("msg", "请填写正确的考点信息!");
            return ret;
        }
        if (StringUtils.isEmpty(place.getName())) {
            ret.put("type", "error");
            ret.put("msg", "考点名称不能为空!");
            return ret;
        }

        if (isExistByName(place.getName())) {
            ret.put("type", "error");
            ret.put("msg", "该考点已经存在!");
            return ret;
        }
        if (placeService.add(place) <= 0) {
            ret.put("type", "error");
            ret.put("msg", "添加失败，请联系管理员!");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "添加成功!");
        return ret;
    }

    /**
     * 客户信息编辑操作
     *
     * @param place
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> edit(Place place) {
        Map<String, String> ret = new HashMap<>();
        if (place == null) {
            ret.put("type", "error");
            ret.put("msg", "请填写正确的考点信息!");
            return ret;
        }
        if (StringUtils.isEmpty(place.getName())) {
            ret.put("type", "error");
            ret.put("msg", "考点名称不能为空!");
            return ret;
        }

        if (isExistByName(place.getName())) {
            ret.put("type", "error");
            ret.put("msg", "该考点已经存在!");
            return ret;
        }
        if (placeService.edit(place) <= 0) {
            ret.put("type", "error");
            ret.put("msg", "添加失败，请联系管理员!");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "修改成功!");
        return ret;
    }

    /**
     * 分页查询客户信息
     *
     * @param name
     * @param page
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> list(
            @RequestParam(name = "name", defaultValue = "") String name,
            @RequestParam(name = "address", defaultValue = "") String address,
            Page page) {
        Map<String, Object> ret = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("name", name);
        queryMap.put("address", address);
        queryMap.put("offset", page.getOffset());
        queryMap.put("pageSize", page.getRows());
        ret.put("rows", placeService.findList(queryMap));
        ret.put("total", placeService.getTotal(queryMap));
        return ret;
    }

    /**
     * @return
     */
    @RequestMapping(value = "/listAll", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> listAll() {
        Map<String, Object> ret = new HashMap<>();
        ret.put("rows", placeService.findAll());
        return ret;
    }

    /**
     * 客户信息删除操作
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> delete(Integer id) {
        Map<String, String> ret = new HashMap<>();
        if (id == null) {
            ret.put("type", "error");
            ret.put("msg", "请选择要删除的信息!");
            return ret;
        }
        try {
            if (placeService.delete(id) <= 0) {
                ret.put("type", "error");
                ret.put("msg", "删除失败，请联系管理员!");
                return ret;
            }
        } catch (Exception e) {
            ret.put("type", "error");
            ret.put("msg", "删除异常，请联系管理员!");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "删除成功!");
        return ret;
    }

    /**
     * 判断用户名是否存在
     *
     * @param name
     * @return
     */
    private boolean isExistByName(String name) {
        Place findByName = placeService.findByName(name);
        if (findByName == null) {
            return false;
        }
        return false;
    }
}
